package com.hk.Mgr.web.controller.order;

import java.util.List;

import com.hk.commons.utils.MsgTypeConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hk.app.utils.SendMsgCilent;
import com.hk.commons.CacheConstants;
import com.hk.commons.ConstantPush;
import com.hk.commons.JsonUtils;
import com.hk.commons.RedisCilent;
import com.hk.commons.StringHelper;
import com.hk.commons.entity.Parameter;
import com.hk.commons.entity.order.OrderInfo;
import com.hk.commons.entity.product.ProductInfo;
import com.hk.commons.entity.sys.SysUser;
import com.hk.service.IPush;
import com.hk.service.order.IParameter;
import com.hk.service.product.IProductInfo;
import com.hk.service.sys.ISysUser;

public class OrderMsgPushQuartz {

	private Logger logger = LoggerFactory.getLogger(OrderMsgPushQuartz.class);

	@Autowired
	public ISysUser user;

	@Autowired
	private SendMsgCilent sendMsgCilent;

	@Autowired
	private IProductInfo productInfo;

	@Autowired
	private IParameter parameterServices;

	@Autowired
	private IPush pushService;

	public void start() {
		List<String> list = RedisCilent.listGet(
				CacheConstants.ORDER_SEND_SMS_PUSH, 0, 9);

		for (String str : list) {

			OrderInfo orderInfo = JsonUtils.toBean(str, OrderInfo.class);
			SysUser userInfo = user.findById(orderInfo.getUserId());
			if(userInfo == null){
				continue;
			}

			String mobile = userInfo.getName();
			String recommendCode = userInfo.getRecommendCode();
			String orderId = String.valueOf(orderInfo.getId());
			String name = userInfo.getRealName();
			ProductInfo producrInfo = productInfo.findById(orderInfo
					.getProductId());

			/**
			 * 购买成功：您已成功购买 1笔1000元的火柴定制1号，感谢您对华财通的支持。如有任何问题，欢迎致电客服热线
			 * 400-102-1010 ，谢谢。
			 * 预约成功：您已成功预约1笔1000元的盛世四海1号，两小时内会有理财师为您服务。感谢您对华财通的支持
			 * 。如有任何问题，欢迎致电客服热线 400-102-1010，谢谢
			 */
			StringBuffer content = new StringBuffer();
			/**
			 * 从缓存中直接获取短信内容
			 */

			Parameter params = parameterServices
					.getByName(CacheConstants.ORDER_MSG_CONTEXT_PUSH);
			
			if(params != null){
				content.append(params.getValue01());
				content.append(StringHelper.myformat.format(orderInfo
						.getSuccessAmount()));
				content.append(params.getValue02());
				content.append(producrInfo.getName());
				content.append(params.getValue03());
	
				logger.info(
						" push msg orderId:{} , name:{} ,  mobile:{},  conent{}, ......",
						orderId, name, mobile, content);
	
				boolean flag = sendMsgCilent.sendMsgContentCustom(mobile,
						String.valueOf(content));
				if (flag) {//短信发送成功则推送理财师
					try {
						sendPushToFinancial(mobile, name,
								StringHelper.myformat.format(orderInfo.getSuccessAmount()),
								producrInfo.getName(), recommendCode);
						sendPushToCustomer(mobile, name,
								StringHelper.myformat.format(orderInfo.getSuccessAmount()),
								producrInfo.getName(), mobile);
					} catch (Exception e) {
						logger.error("购买订单推送给理财师异常！",e);
					}
					RedisCilent.delListVal(CacheConstants.ORDER_SEND_SMS_PUSH, 1,
							str);
				}
			}
			
			

		}

	}

	private void sendPushToCustomer(String custPhone, String custName, String amt, String productName, String phone) {
		// 您已成功购买一笔50000元的久恒福海（180天）002，请到投资资产或交易记录中查看
		if (StringHelper.isNotEmpty(phone)) {
			String msg = String.format("您已成功购买一笔%s元的%s，请到投资资产或交易记录中查看", amt, productName);
			pushService.jgPushCustomerMsg(msg, "购买成功", null, null, null, phone, ConstantPush.PUSH_MENU_ORDER, MsgTypeConstants.BUY);
		}
	}

	private void sendPushToFinancial(String custPhone, String custName,
			String amt, String productName, String phone) {
		// // 您的客户张三 ，已成功购买1笔 50000元的火柴定制1号
		if (StringHelper.isNotEmpty(phone)) {
			String msg = ConstantPush.PUSH_ORDER_START + custName + ConstantPush.PUSH_ORDER_MIDDLE + amt
					+ ConstantPush.PUSH_ORDER_END + productName;
			pushService.jgPushMsg(msg, null, null, null, phone,
					ConstantPush.PUSH_MENU_ORDER, MsgTypeConstants.BUY);
		}

	}

	public void run() {
		try {
			this.start();
			// this.sendPushToFinancial();
		} catch (Exception e) {
			logger.error(getClass() + " quartz error: ", e);
		}
	}

}
